Button View allows User to click on the button in order to perform an Action and it contains two parts
● action parameter defines code that should be executed hen Button is pressed
● body defines Button's appearance since it can contain other Views: Text, Stack
Related tutorial Bind View to Action.
Syntax
Button ("Button") { print("Button was pressed") }
Button (action: { print("Button was pressed") } ) { Text("Button") }
In this simplest form Button appearance is defined with the simple String.
After that you define code which should be executed when Button is pressed.
Code
Button("Button") {
print("Button was pressed")
}
This example shows how to define Button appearance with other Views (in this case a singe Text View).
Code
Button(action: {
print("Button was pressed")
}) {
Text("Button")
}
View
Debug Area
Button was pressed